﻿using System;
using System.IO;
using UnityEngine;

public class PackagerRules : MonoBehaviour
{
    [Tooltip("忽略路径")]
    public string[] ignorePaths;
    [Tooltip("忽略扩展名")]
    public string[] ignoreExterns;
    [Tooltip("忽略文件")]
    public string[] ignoreFiles;
    [Tooltip("打包路径")]
    public string[] packingPaths;
    [Tooltip("忽略文件夹")]
    public string[] ignoreFolders;
    
    [Tooltip("无压缩路径（如，视频、配置等）")]
    public string[] dir2NoCompress;
    [Tooltip("无压缩文件扩展名")]
    public string[] extern2NoCompress;
    [Tooltip("指定文件打成独立AssetBundle")]
    public string[] file2AssetBundles;
    [Tooltip("目录下所有文件各自打成独立AssetBundle")]
    public string[] childFile2AssetBundles;
    [Tooltip("指定目录下所有文件打包成一个AssetBundle")]
    public string[] dir2AssetBundles;
    [Tooltip("目录下所有一级子目录都各自打成独立AssetBundle")]
    public string[] childDir2AssetBundles;
    
    [Tooltip("启动场景")]
    public string[] startupScenes;
    [Tooltip("打出的应用文件名")]
    public string appName;
    [Tooltip("AssetBundle的扩展名")]
    public string bundleExtension;
    
    // 根据文件名获取AssetBundle名字
    public string File2AssetBundleName(string filePath)
    {
        // string fileName = Path.GetFileName(filePath);
        string fileDir = filePath.Substring(0, filePath.LastIndexOf('/'));
        // Debug.Log("^^^^  " + fileDir);
        
        // 场景文件处理
        string fileExtern = Path.GetExtension(filePath);
        if (fileExtern.Equals(".unity", StringComparison.OrdinalIgnoreCase))
        {
            return (filePath + bundleExtension).ToLower();
        }

        // 规则1 指定文件打成独立AssetBundle
        if (file2AssetBundles.Length > 0)
        {
            for (int i = 0; i < file2AssetBundles.Length; i++)
            {
                if (file2AssetBundles[i].Equals(filePath, StringComparison.OrdinalIgnoreCase))
                {
                    return (file2AssetBundles[i] + bundleExtension).ToLower();
                }
            }
        }
        
        // 规则2 目录下所有文件各自打成独立AssetBundle
        if (childFile2AssetBundles.Length > 0)
        {
            for (int i = 0; i < childFile2AssetBundles.Length; i++)
            {
                if (fileDir.Equals(childFile2AssetBundles[i], StringComparison.OrdinalIgnoreCase))
                {
                    return (filePath + bundleExtension).ToLower();
                }
            }
        }
        
        // 规则3 目录下所有文件打成一个AssetBundle
        if (dir2AssetBundles.Length > 0)
        {
            for (int i = 0; i < dir2AssetBundles.Length; i++)
            {
                if (fileDir.Equals(dir2AssetBundles[i], StringComparison.OrdinalIgnoreCase))
                {
                    return (fileDir + bundleExtension).ToLower();
                }
            }
        }
        
        // 规则4 目录下所有一级子目录各自打成独立AssetBundle (必须是一级子目录，不包括一级子文件)
        if (childDir2AssetBundles.Length > 0)
        {
            for (int i = 0; i < childDir2AssetBundles.Length; i++)
            {
                // 文件的上一级的上一级文件夹名字
                string fileDirUpDir = fileDir.Substring(0, fileDir.LastIndexOf('/'));

                if (fileDirUpDir.Equals(childDir2AssetBundles[i], StringComparison.OrdinalIgnoreCase))
                {
                    return (fileDir + bundleExtension).ToLower();
                }
            }
        }

        // 规则5 默认按根节点目录打成AssetBundle
        return (fileDir + bundleExtension).ToLower();
    }
    
    // 是否是要打成AssetBundle资源
    public bool NeedCompress(string filePath)
    {
        // string fileName = Path.GetFileName(filePath);
        string fileDir = filePath.Substring(0, filePath.LastIndexOf('/'));
        
        // 规则 指定文件不再压缩，直接copy
        for (int i = 0; i < dir2NoCompress.Length; i++)
        {
            if (fileDir.Equals(dir2NoCompress[i], StringComparison.OrdinalIgnoreCase))
                return false;
        }

        return true;
    }
    
    // 判断文件后缀是否符合不压缩文件后缀
    public bool FileWithNoCompressExtern(string filePath)
    {
        string extension = Path.GetExtension(filePath);
        
        // 规则 指定文件是否符合不压缩文件后缀
        for (int i = 0; i < extern2NoCompress.Length; ++i)
        {
            if (string.Equals(extension, extern2NoCompress[i], StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
        }

        return false;
    }
}